package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.AccountInfoMapper;
import com.gxa.p2p.mapper.AssetDetailsMapper;
import com.gxa.p2p.mapper.RepaymentDetailMapper;
import com.gxa.p2p.mapper.vRepaymentInfoMapper;
import com.gxa.p2p.pojo.AccountInfo;
import com.gxa.p2p.pojo.AssetDetails;
import com.gxa.p2p.pojo.RepaymentDetail;
import com.gxa.p2p.pojo.vRepaymentInfo;
import com.gxa.p2p.service.RepaymentDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RepaymentDetailServiceImpl implements RepaymentDetailService {
    @Resource
    vRepaymentInfoMapper vRepaymentInfoMapper; //

    @Resource
    RepaymentDetailMapper repaymentDetailMapper; // 还款记录信息
    @Resource
    AccountInfoMapper accountInfoMapper; // 用户信息
    @Resource
    AssetDetailsMapper assetDetailsMapper; // 账户流水

    @Override
    public List<Object> findInfo(Integer userId, Integer page, Integer limit) {
        List<Object> list = new ArrayList<>();

        List<vRepaymentInfo> vRepaymentInfoList = vRepaymentInfoMapper.selectByLimit(userId, (page - 1) * limit, limit);
        list.add(count(userId));
        list.add(vRepaymentInfoList);
        return list;
    }

    @Override
    public List<Object> findInfoDim(Integer userId, String pri, Integer page, Integer limit) {
        List<Object> list = new ArrayList<>();

        List<vRepaymentInfo> vRepaymentInfoList = vRepaymentInfoMapper.selectByLimitDim(userId, "%"+pri+"%",(page - 1) * limit, limit);
        System.out.println();
        vRepaymentInfo vRepaymentInfo = vRepaymentInfoMapper.selectByCountDim(userId, "%"+pri+"%");
        System.out.println(vRepaymentInfo.toString());
        list.add(vRepaymentInfo.getCount());
        list.add(vRepaymentInfoList);
        return list;
    }

    @Override
    public Boolean actualUpdate(vRepaymentInfo vRepaymentInfo) {
        int sum = 0;
        // 更新accountInfo 可用金额(暂时不考虑)/冻结金额/待还总金额/剩余信用额度/总额度
        AccountInfo accountInfo = accountInfoMapper.selectByUserId(vRepaymentInfo.getUserId());
//        accountInfo.setUsableAmount(accountInfo.getUsableAmount().subtract(vRepaymentInfo.getRepaymentMoney()));
        accountInfo.setFrozenAmount(accountInfo.getFrozenAmount().subtract(vRepaymentInfo.getRepaymentMoney()));
        accountInfo.setUnreturnAmount(accountInfo.getUnreturnAmount().subtract(vRepaymentInfo.getRepaymentMoney()));
        accountInfo.setRemainCredits(accountInfo.getRemainCredits().add(vRepaymentInfo.getRepaymentMoney()));
        accountInfo.setTotalCredits(accountInfo.getTotalCredits().add(vRepaymentInfo.getRepaymentMoney()));
        sum += accountInfoMapper.updateByPrimaryKeySelective(accountInfo);
        // 更新assetDetails 可用金额/支出
        AssetDetails assetDetails = assetDetailsMapper.selectByPrimaryKey(vRepaymentInfo.getUserId());
        assetDetails.setUsableAmount(assetDetails.getUnreturnAmount().subtract(vRepaymentInfo.getRepaymentMoney()));
        assetDetails.setExpense(assetDetails.getExpense().add(vRepaymentInfo.getRepaymentMoney()));
        assetDetails.setFrozenAmount(assetDetails.getFrozenAmount().subtract(vRepaymentInfo.getRepaymentMoney()));
        sum+=assetDetailsMapper.updateByPrimaryKeySelective(assetDetails);
        // 更新还款记录表
        RepaymentDetail repaymentDetail = repaymentDetailMapper.selectByPrimaryKey(vRepaymentInfo.getId());
        repaymentDetail.setStatus(vRepaymentInfo.getStatus());
        repaymentDetail.setRepaymentActualDate(new Date());
        repaymentDetail.setRepaymentType(vRepaymentInfo.getRepaymentType());
        repaymentDetail.setRepaymentUser(vRepaymentInfo.getRepaymentUser());
        sum += repaymentDetailMapper.updateByActual(repaymentDetail);
        return sum >=3;
    }

    // 统计数量
    private Integer count(Integer userId) {
        return vRepaymentInfoMapper.selectByCount(userId).getCount();
    }
}
